package com.dpad.crmclientapp.android.modules.newapp.ui.fragment

import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter

import com.dpad.crmclientapp.android.modules.newapp.presenter.MessageFragmentPresenter
import com.dpad.telematicsclientapp.adapter.OutMessageAdapter

import com.dpad.telematicsclientapp.android.R
import com.dpad.telematicsclientapp.mvp.kit.Kits
import com.dpad.telematicsclientapp.netlibrary.Contances
import com.dpad.telematicsclientapp.netlibrary.base.basecopy.BaseListFragment
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.OutMessageBean
import com.dpad.telematicsclientapp.ui.HomeActivity
import com.dpad.telematicsclientapp.util.TitleToCode
import com.dpad.telematicsclientapp.util.ViewUtils
import com.dpad.telematicsclientapp.util.utils.DialogUtil
import kotlinx.android.synthetic.main.fragment_messge_list.*
import rx.subscriptions.CompositeSubscription
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.Result

/**
 * ================================================
 * 作    者：booob
 * 版    本：1.0
 * 创建日期：2019-04-15-0015 10:28
 * 描    述：
 * 修订历史：
 * ================================================
 */
class MessageFragment : BaseListFragment<MessageFragmentPresenter>(), BaseQuickAdapter.OnItemChildClickListener {
    private var tag = 0;
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        when (view?.id) {
            R.id.tv_item_msg_delet -> {
                DialogUtil.showBasicDialog(context, "删除",
                        "确认删除?") { dialog, confirm ->
                    if (confirm) {
//                        adapter?.notifyItemChanged(position)
                        getP().clearMessage("1", "0", "0", data[position].type, true)
                    }
                    dialog.dismiss()
                }
            }
            R.id.ll_item_msg_content -> {
//                val intent = Intent(context, SysMessageCenterActivity::class.java)
//                intent.putExtra("type", data[position].type)
//                startActivityForResult(intent, 111)

                ARouter.getInstance()
                        .build(Contances.URL_ACTIVITY_SYSMESSAGE, Contances.GROUP_OTHER)
                        .withString("type", data[position].type)
                        .navigation(context, 111)

            }
        }
    }

    private var data: MutableList<Result> = mutableListOf()

    override fun getRecyclerAdapter(): BaseQuickAdapter<*, *> {
        return OutMessageAdapter(data)
    }

    override fun refresh() {
        val map = sortedMapOf(Pair("", "" as Any))
        p.getMessageData(map)
    }

    override fun loadMore() {
        return
    }

    override fun initPageData() {
//        tvLayerHead.text = "消息中心"

        ViewUtils.setStatusView(context, statusView)
        if (mEmptyIm != null) {
            mEmptyIm.setImageResource(R.mipmap.no_message)
            setDescription("暂无通知")
        }
        adapter.onItemChildClickListener = this
        //加载数据
//        refresh()
    }


    override fun newPresenter(subscriptions: CompositeSubscription?): MessageFragmentPresenter {
        return MessageFragmentPresenter()
    }

    override fun canLoadMore(): Boolean {
        return false
    }

    /**
     *请求接口完成后展示数据
     */
    fun showData(bean: OutMessageBean) {
        if (!Kits.Empty.check(bean)) {
            data.clear()
            if (!Kits.Empty.check(bean.result) && bean.result.isNotEmpty()) {
                /*
                去掉了远控的消息
                 */
//                for (item in bean.result) {
//                    if (item.type != Constant.MESSAGESETTING_YCKZ) {
//                        data.add(item)
//                    }
//                }
                data.addAll(bean.result as Collection<Result>)
            }
            adapter.notifyDataSetChanged()
        } else {
//            showNoData()
        }
        for(resultInstance in bean.result){
            if(resultInstance.num>0){
                tag = 1;
            }
        }
        var homeActivity = this.activity as HomeActivity;
        if(tag==1){
            homeActivity.showPoint(true)
        }else{
            homeActivity.showPoint(false)
        }
        tag = 0;
    }

    /**
     * 请求接口失败后展示数据
     */
    fun showNoData() {
        data.clear()
        for (i in 0..4) {
            val result: Result = Result("", 0, "", "", i.toString())
            data.add(result)
        }
        adapter.notifyDataSetChanged()
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_messge_list
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        onRefresh()
    }

    override fun visiableLoadData() {
        refresh()
    }

    override fun isTFragment(): Boolean {
        return true
    }

    override fun getPageTitle(): String {
        return TitleToCode.TYPE3.title
    }
}